package net.neptech.listener;

import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import net.neptech.enums.workflow.InstanceConstant;
import net.neptech.util.FlowableUtil;
import org.flowable.engine.TaskService;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;

/**
 * @Author Saya
 * @Date 2025/8/24 18:08
 * @Description TODO
 **/
@Slf4j
public class ApprovalCreateListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        TaskService taskService = SpringUtil.getBean(TaskService.class);

        String assignee = delegateTask.getAssignee();
        String name = delegateTask.getName();
        log.info("审批人创建了 任务{}-执行人:{}", name, assignee);
        String processInstanceId = delegateTask.getProcessInstanceId();
        TaskEntityImpl taskEntity = (TaskEntityImpl) delegateTask;

        String nodeId = taskEntity.getTaskDefinitionKey();
        String processDefinitionId = taskEntity.getProcessDefinitionId();
        //流程id
        String flowId = FlowableUtil.getFlowId(processDefinitionId);

        //设置流程唯一id
        String taskId = taskEntity.getId();
        taskService.setVariableLocal(taskId, InstanceConstant.VariableKey.FLOW_UNIQUE_ID, FlowableUtil.getFlowUniqueId(nodeId, flowId, processInstanceId, delegateTask.getTenantId()));
    }
}
